package com.autumn.aitraffic.controller;

import com.autumn.aitraffic.api.Result;
import com.autumn.aitraffic.domain.po.CameraRecords;
import com.autumn.aitraffic.domain.po.Jitteralarm;
import com.autumn.aitraffic.domain.vo.CameraDetailVo;
import com.autumn.aitraffic.domain.vo.CameraNumVo;
import com.autumn.aitraffic.domain.vo.CameraResultVo;
import com.autumn.aitraffic.service.impl.CameraRecordsServiceImpl;
import com.autumn.aitraffic.service.impl.JitteralarmServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@RestController()
@RequestMapping("/camera")
public class CameraController {
    @Autowired
    CameraRecordsServiceImpl cameraRecordsService;
    @Autowired
    JitteralarmServiceImpl jitteralarmService;
    @GetMapping
    public Result getcamera(){
        List<Jitteralarm> list = jitteralarmService.lambdaQuery().list();
        List<CameraRecords> list1 = cameraRecordsService.lambdaQuery().list();
        Map<String, String> originMap = list1.stream().collect(Collectors.toMap(CameraRecords::getName, CameraRecords::getOriginalImage));
        ArrayList<CameraDetailVo> cameraDetailVos = new ArrayList<>();
        CameraNumVo cameraNumVo = new CameraNumVo();
        for (Jitteralarm jitteralarm : list) {
            CameraDetailVo cameraDetailVo = new CameraDetailVo();
            cameraDetailVo.setId(String.valueOf(jitteralarm.getId()));
            cameraDetailVo.setName(jitteralarm.getIntersection()+"号路口");
            if (!originMap.containsKey(jitteralarm.getIntersection())) continue;
            cameraDetailVo.setOriginalImage(originMap.get(jitteralarm.getIntersection()));
            cameraDetailVo.setCurrentImage(jitteralarm.getPicUrl());
            cameraDetailVo.setStatus(jitteralarm.getAlarm()? 1:0);
            cameraDetailVos.add(cameraDetailVo);
            if (jitteralarm.getAlarm()) cameraNumVo.setOffset(cameraNumVo.getOffset()+1);
            else cameraNumVo.setNormal(cameraNumVo.getNormal()+1);
            cameraNumVo.setSum(cameraNumVo.getSum()+1);
        }
        CameraResultVo cameraResultVo = new CameraResultVo();
        cameraResultVo.setList(cameraDetailVos);
        cameraResultVo.setNum(cameraNumVo);
        return Result.succ(null,cameraResultVo);
    }
}
